#include "EpilIfStatementCode.h"
#include "EpilOutputHelper.h"
#include "TCharSupport.h"
#include "EpilParserException.h"
#include "EpilPointerHelper.h"
#include "EpilPointerHelper.h"


EpilIfStatementCode::EpilIfStatementCode( const EpilLexLocation & location )
: EpilStatementCodeBase( location )
, m_ifBlock( NULL )
, m_elseBlock( NULL )
{
}


EpilIfStatementCode::~EpilIfStatementCode()
{
	EpilPointerHelper::SafeDelete( m_ifBlock );
	EpilPointerHelper::DeleteItemsAndClear( m_elseifBlocks );
	EpilPointerHelper::SafeDelete( m_elseBlock );
}


tstring EpilIfStatementCode::ToEpilCode( int indent ) const
{
	tstringstream ret;
	ret << EpilOutputHelper::GetIndent( indent );
	ret << t("if ");
	ret << m_ifBlock->ToEpilCode( indent );

	if( m_elseifBlocks.empty() == false )
	{
		for( EpilThenBlockCodeList::const_iterator it = m_elseifBlocks.begin(); it != m_elseifBlocks.end(); ++it )
		{
			ret << endl;
			ret << EpilOutputHelper::GetIndent( indent );
			ret << t("elseif ");
			ret << (*it)->ToEpilCode( indent );
		}
	}

	if( m_elseBlock )
	{
		ret << endl;
		ret << EpilOutputHelper::GetIndent( indent );
		ret << t("else");
		ret << endl;
		ret << m_elseBlock->ToEpilCode( indent + 1 );
	}

	ret << endl;
	ret << EpilOutputHelper::GetIndent( indent );
	ret << t("end");

	return ret.str();
}